package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test4 {
    public static void main(String[] args) throws URISyntaxException, IllegalAccessException, InstantiationException, ClassNotFoundException, InvocationTargetException {
        File dir = new File(
                Test3.class.getResource(".").toURI()
        );
        System.out.println(dir);

        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub:subs){
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.lastIndexOf("."));
            String packName = Test3.class.getPackage().getName();
            className = packName + "." + className;
            Class cls = Class.forName(className);
            if(cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println("实例化:"+className);
                Object o = cls.newInstance();
                System.out.println(o);

                Method[] methods = cls.getDeclaredMethods();
            for(Method method:methods){
                if(method.isAnnotationPresent(AutoRunMethod.class)){
                    System.out.println("开始调用方法:" + method.getName());
                    method.invoke(o);
                }
            }
          }
        }
    }


}
